import { Actor, Action } from 'iflux2';
import { List } from 'immutable';
import { CommentType } from '../constant/action-type';

export default class CommentActor extends Actor {

    defaultState() {
        return {
            // 加载订单列表时，评价相关的数据也需加载出来
            comments: {},  // 当前页所有评价数据, key为oid, value为Comment
            replies: {},   // 当前页所有评价回复, key为commentId, value为该评价下的回复列表CommentReply[]
            suns: {},      // 当前页所有评价晒单, key为commentId, value为该评价下的晒单列表CommentSun[]
            // 单条评价详情数据
            comment: {},
            commentSuns: [],
            commentReplies: []
        }
    }

    @Action(CommentType.INIT_COMMENT_LIST)
    initCommentList(state, comments) {
        return state.set('comments', comments)
    }

    @Action(CommentType.INIT_REPLY_LIST)
    initReplyList(state, replies) {
        return state.set('replies', replies)
    }

    @Action(CommentType.INIT_SUN_LIST)
    initSunList(state, suns) {
        return state.set('suns', suns)
    }

    @Action(CommentType.SET_COMMENT_DETAIL)
    setCommentDetail(state, oid) {
        const comment = state.get('comments').get(oid);
        return state.set('comment', comment)
            .set('commentSuns', state.get('suns').get(comment.get('id')))
            .set('commentReplies', state.get('replies').get(comment.get('id')))
    }

    @Action(CommentType.SET_COMMENT_STATE)
    updateCommentState(state, { oid, commentId, showState }) {
        let comment = state.get('comments').get(oid);
        comment = comment.set('showState', showState);
        return state.set('comment', comment)
            .set('comments', state.get('comments').set(oid, comment))
    }

    @Action(CommentType.SET_REPLY_STATE)
    updateReplyState(state, { commentId, id, showState }) {
        let commentReplies = state.get('replies').get(commentId);
        commentReplies = commentReplies.map(reply => reply.get('id') === id ? reply.set('showState', showState) : reply)
        return state.set('replies', state.get('replies').set(commentId, commentReplies))
            .set('commentReplies', commentReplies)
    }

    @Action(CommentType.ADD_COMMENT_REPLY)
    addCommentReply(state, commentReply) {
        let replies = state.get('replies');
        const commentId = commentReply.get('commentId');
        replies = replies.set(commentId, !replies.get(commentId) ? List.of(commentReply) : replies.get(commentId).push(commentReply));
        return state.set('replies', replies)
            .set('commentReplies', replies.get(commentId));
    }

    @Action(CommentType.DELETE_COMMENTS)
    deleteComments(state, { oids }) {
        let deleteCommentIds = oids.map(oid => state.get('comments').get(oid).get('id'));
        return state.set('comments', state.get('comments').filter((value, key) => oids.indexOf(key) < 0))
            .set('replies', state.get('replies').filter((value, key) => deleteCommentIds.indexOf(key) < 0))
            .set('suns', state.get('suns').filter((value, key) => deleteCommentIds.indexOf(key) < 0))
    }

};
